<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
require_once (dirname ( __FILE__ ) . "/../../PHPExcel/PHPExcel.php");
class Promoter extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	/*
	 * 返回推广员列表
	 */
	public function index() {
		$this->load->model ( 'promoter_model' );
		$data ['promoters'] = $this->promoter_model->find_all_promoters ();
		$data ['title'] = '推广员管理 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'promoter/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回添加推广员页面
	 */
	public function add() {
		$data ['title'] = '添加推广员 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'promoter/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回编辑推广员页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'promoter_model' );
		$data ['promoter'] = $this->promoter_model->load ( $id );
		$data ['title'] = '编辑推广员 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'promoter/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存推广员信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$promoter = array (
				'name' => $this->input->post ( 'name' ),
				'gender' => $this->input->post ( 'gender' ),
				'phone' => $this->input->post ( 'phone' ),
				'type' => $this->input->post ( 'type' ),
				'account' => $this->input->post ( 'account' )
		);
		$this->load->model ( 'promoter_model' );
		if ($id) {
			if($this->promoter_model->update ( $id, $promoter )){
				return_json(TRUE,'','修改成功！'); 
			}else{
				return_json(FALSE,'','信息未变动！');
			}
		} else {
			$promoterid=$this->promoter_model->create ( $promoter );
			if(!empty($promoterid)){
				return_json(TRUE,'','添加成功！'); 
			}else{
				return_json(FALSE,'','添加失败！');
			}
		}
	}
	/*
	 * 删除推广员信息
	 */
	public function delete() {
		$id = $this->input->post('id');
		$this->load->model ( 'promoter_model' );
		if ($this->promoter_model->delete ( $id )) {
			return_json(TRUE,'','删除成功！'); 
		}else{
			return_json(TRUE,'','删除失败！');
		}
	}
	/*
	 * 导出提成表(待修改)
	 */
	public function exportCuts() {
		$startdate = $this->uri->segment(4);
		$enddate = $this->uri->segment(6);
		$resultPHPExcel = new PHPExcel ();
		$objActSheet = $resultPHPExcel->getActiveSheet();
		//$objActSheet->setTitle('支出统计');
		$objActSheet->setCellValue ( 'A1', '日期：'.date ( "Y-m-d", strtotime($startdate)).'至'.date ( "Y-m-d", strtotime($enddate)) );
		$this->load->model ( 'promoter_model' );
		$this->load->model ( 'user_model' );
		$this->load->model ( 'order_model' );
		$promoters=$this->promoter_model->find_all_promoters();
		$i=2;
		foreach($promoters as $promoter){
			$objActSheet->setCellValue ( 'A'.$i, '推广员：'.$promoter['name']);
			$i++;
			$j=$i;
			$objActSheet->setCellValue ( 'A'.$i, '商家');
			$objActSheet->setCellValue ( 'B'.$i, '下单次数');
			$objActSheet->setCellValue ( 'C'.$i, '采购额(元)');
			$objActSheet->setCellValue ( 'D'.$i, '首单日期');
			$objActSheet->setCellValue ( 'E'.$i, '提成比例');
			$objActSheet->setCellValue ( 'F'.$i, '提成额(元)');
			$i++;
			$users=$this->user_model->find_by_promoter($promoter['id']);
			$sum=0;
			foreach($users as $user){
				$firstorder=$this->order_model->get_firstorder_by_user($user['id']);
				if(empty($firstorder)){
					continue;
				}
				$firstdate=$firstorder['order_time'];
				$lastdate=date('Y-m-d', strtotime(date('Y-m-01', strtotime($firstdate)) . ' +2 month -1 day'));//第二月的最后一天
				if(strtotime(date("Y-m-d",strtotime($firstdate)))>=strtotime($startdate)&&strtotime(date("Y-m-d",strtotime($firstdate)))<=strtotime($enddate)){
					$objActSheet->setCellValue ( 'A'.$i, $user['shopname'].'(新)');
				}else{
					$objActSheet->setCellValue ( 'A'.$i, $user['shopname']);
				}
				if(strtotime($enddate)<=strtotime($lastdate)){
					$order=$this->order_model->get_total_by_user_date($user['id'],$startdate,$enddate);
					$num=$this->order_model->get_num_by_user_date($user['id'],$startdate,$enddate);
					if(!empty($order['total'])){
						$total=$order['total'];
						$objActSheet->setCellValue ( 'B'.$i, $num);
						$objActSheet->setCellValue ( 'C'.$i, $total);
						$objActSheet->setCellValue ( 'D'.$i, date('Y-m-d', strtotime($firstdate)));
						$objActSheet->getStyle('E'.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00);
						$objActSheet->setCellValue ( 'E'.$i, '0.02');//提成比例改为2%
						$objActSheet->setCellValue ( 'F'.$i, $total*0.02);
						$sum+=$total*0.02;
						$i++;
					}
				}else{
					$order=$this->order_model->get_total_by_user_date($user['id'],$startdate,$enddate);
					$num=$this->order_model->get_num_by_user_date($user['id'],$startdate,$enddate);
					if(!empty($order['total'])){
						$total=$order['total'];
						$objActSheet->setCellValue ( 'B'.$i, $num);
						$objActSheet->setCellValue ( 'C'.$i, $total);
						$objActSheet->setCellValue ( 'D'.$i, date('Y-m-d', strtotime($firstdate)));
						$objActSheet->getStyle('E'.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00);
						$objActSheet->setCellValue ( 'E'.$i, '0.005');//提成比例为0.5%
						$objActSheet->setCellValue ( 'F'.$i, $total*0.005);
						$sum+=$total*0.005;
						$i++;
					}
				}
			}
			$objActSheet->setCellValue ( 'A'.$i, '总计：');
			$objActSheet->setCellValue ( 'F'.$i, $sum);
			$objActSheet->getStyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);//合计右对齐
			$styleArray = array(//加入边框
					'borders' => array(
							'allborders' => array(
									'style' => PHPExcel_Style_Border::BORDER_THIN
							),
					),
			);
			$objActSheet->getStyle('A'.$j.':F'.$i)->applyFromArray($styleArray);
			$i+=2;
		}
		// 设置列宽
		$objActSheet->getColumnDimension ( 'A' )->setWidth ( 15);
		$objActSheet->getColumnDimension ( 'B' )->setWidth ( 15 );
		$objActSheet->getColumnDimension ( 'C' )->setWidth ( 15 );
		$objActSheet->getColumnDimension ( 'D' )->setWidth ( 15 );
		$objActSheet->getColumnDimension ( 'E' )->setWidth ( 15 );
		$objActSheet->getColumnDimension ( 'F' )->setWidth ( 15 );
			
		$outputFileName = '提成表.xls';
		$xlsWriter = new PHPExcel_Writer_Excel5 ( $resultPHPExcel );
		// ob_start(); ob_flush();
		header ( "Content-Type: application/force-download" );
		header ( "Content-Type: application/octet-stream" );
		header ( "Content-Type: application/download" );
		header ( 'Content-Disposition:inline;filename="' . $outputFileName . '"' );
		header ( "Content-Transfer-Encoding: binary" );
		header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
		header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
		header ( "Pragma: no-cache" );
		$xlsWriter->save ( "php://output" );
	}
}